package com.example.interview.no0202.solution;

import com.example.interview.no0202.ListNode;
import com.example.interview.no0202.Solution;

/**
 * @Description 解答接口-返回倒数第 k 个节点
 * @ClassName SolutionImpl
 * @Author Administrator
 * @Date 2020/7/9 14:43
 * @Version 1.0.0
 */
public class SolutionImpl implements Solution {
    @Override
    public int kthToLast(ListNode head, int k) {
        int count = 1;
        ListNode currentNode = head;
        while (currentNode.next != null){
            count++;
            currentNode = currentNode.next;
        }
        currentNode = head;
        for (int i = 0; i < count-k; i++) {
            currentNode = currentNode.next;
        }
        return currentNode.val;
    }
}
